####
# Based partly on https://www.archlinux.org/packages/extra/x86_64/gnome-calculator/
####
# Past Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_realname=gnome-calculator
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=44.0
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
pkgdesc="GNOME desktop calculator (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gtksourceview5"
         "${MINGW_PACKAGE_PREFIX}-libadwaita"
         "${MINGW_PACKAGE_PREFIX}-libgee"
         "${MINGW_PACKAGE_PREFIX}-libhandy"
         "${MINGW_PACKAGE_PREFIX}-libsoup3"
         "${MINGW_PACKAGE_PREFIX}-mpc"
         "${MINGW_PACKAGE_PREFIX}-mpfr")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-vala"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-appstream-glib"
             "${MINGW_PACKAGE_PREFIX}-yelp-tools") # Used in package step
license=('spdx:GPL-3.0-or-later')
url="https://wiki.gnome.org/Apps/Calculator"
source=(https://download.gnome.org/sources/${_realname}/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
        001-win.patch
)
sha256sums=('14e763329f88309a7e152780d57361b543100e323906b34e0655fdc315b71043'
            '4cf4eefa6cfef044ec4f068daa8d427f58d65acc3b6d854eb4a1dea1bb4ad6f2')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  del_file_exists "${srcdir}/${_realname}-${pkgver}/lib/windows.c"
  apply_patch_with_msg 001-win.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  # clang error from vala generated file
  export CFLAGS="${CFLAGS} -Wno-incompatible-function-pointer-types"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --prefix="${MINGW_PREFIX}" \
    --wrap-mode=nodownload \
    --buildtype plain \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson.exe compile
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/meson.exe test || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
